/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.twistinjava.adminDAO;

import com.twistinjava.exception.TwistinjavaException;
import com.twistinjava.logon.User;
import com.twistinjava.util.DBManager;
import com.twistinjava.util.LoggableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.logging.*;

/**
 *
 * @author shikhar
 */
public class AdminDAO
{

    private static Log log = LogFactory.getLog(AdminDAO.class);

    public void saveuser(User user) throws TwistinjavaException, SQLException
    {
        log.debug("inside saveuser of AdminDAO class ********");
        Connection conn = null;
        StringBuilder sqlQry = new StringBuilder();
        PreparedStatement pStmt = null;
        try
        {
            conn = DBManager.getConnection();
            sqlQry.append("insert into User (firstName, lastName, emailID, logonID, password, isActivated)").append("values (?,?,?,?,?,?)");
            pStmt = new LoggableStatement(conn, sqlQry.toString());
            pStmt.setString(1, user.getFirstName());
            pStmt.setString(2, user.getLastName());
            pStmt.setString(3, user.getEmail());
            pStmt.setString(4, user.getLogonID());
            pStmt.setString(5, user.getPassword());
            pStmt.setBoolean(6, true);
            pStmt.execute();
        }
        catch (SQLException sQLException)
        {
            log.error("Exception inside saveUser(User user) method of AdminDAO class ********" + sQLException.getMessage());
            throw new TwistinjavaException(sQLException.getMessage());
        }
        finally
        {
            try
            {
                if (pStmt != null)
                {
                    pStmt.close();
                }
                if(conn != null)
                {
                    conn.close();
                }
            }
            catch (SQLException sqle)
            {
                log.error("Inside AdminDAO :saveUser(User user) method of AdminDAO class :" +sqle.getMessage());
            }

        }
    }

   
}
